package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private SpuDetailService spuDetailService;
    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;

// 分页查询
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> quryPaGe(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                    @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                                    @RequestParam(value = "saleable", required = false) Boolean saleable,
                                                    @RequestParam(value = "categoryId", required = false) Long categoryId,
                                                    @RequestParam(value = "brandId", required = false) Long brandId,
                                                    @RequestParam(value = "id", required = false) Long id){

        return ResponseEntity.ok(spuService.quryPaGe(page,rows,saleable,categoryId,brandId,id));
    }

    //新增商品
    @PostMapping
    public ResponseEntity<Void> add(@RequestBody SpuDTO spuDTO){
        spuService.add(spuDTO);
    return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    //更新商品
    @PutMapping
    public  ResponseEntity<Void>updateSpu(@RequestBody SpuDTO spuDTO){
        spuService.updateSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //上架下架商品
    @PutMapping("/saleable")
    public ResponseEntity<Void>RackDownModify(@RequestParam("id") Long id,
                                              @RequestParam("saleable") Boolean saleable){

        spuService.RackDownModify(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //根据id查询商品spu
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(new SpuDTO(spuService.getById(id)));
    }
    //根据id批量查询sku
  @GetMapping("/sku/list")
  public ResponseEntity<List<SkuDTO>>SkuByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(skuService.SkuByIds(ids));
  }
  @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>>sSkuByIdlist(@RequestParam("id")Long id){

      return ResponseEntity.ok(skuService.queryById(id));
  }

    //根据id查询商品详情
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO>queryById(@RequestParam("id")Long id){
    return ResponseEntity.ok(spuDetailService.queryById(id));
    }


    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){
        return ResponseEntity.ok(spuService.queryGoodsById(id));
    }
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>>querySpecsValues(@RequestParam("id") Long id, @RequestParam(value = "searching", required = false) Boolean searching){
        return ResponseEntity.ok(spuDetailService.querySpecValues(id, searching));
    }
    //扣减库存
    @PutMapping("/stock/minus")
    public ResponseEntity<Void>deductStock(@RequestBody Map<Long,Integer> skuMap){
        skuService.deductStock(skuMap);
        return ResponseEntity.noContent().build();
    }

    /**
     * 恢复库存
     * @param skuMap sku信息，key：skuId，value：扣减数量
     */
    @PutMapping("/stock/plus")
    public ResponseEntity<Void> plusStock(@RequestBody Map<Long, Integer> skuMap){
        skuService.plusStock(skuMap);
        return ResponseEntity.noContent().build();
    }
}